//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Utils/Base/Meta.Common.h"
#include "UI/MetaData/Meta.Widgets.h"
#include "Utils/UI/UILocalization.h"

namespace Engine
{
namespace Utils
{

	//
	// UI Frame
	//

	class UIFrame : public MetaData::FrameCfg
	{
	// types
	private:
		typedef MetaData::FrameCfg	Frame;

		typedef vec2 & (* ConvertAxis_t) (vec2 &value, const vec2 &size);


	// variables
	private:
		int		_designAxisDirection;

		
	// metadata
	public:
		static CMetaClass &		MetaUIFrame ()	{ static CMetaClass metaClass;  return metaClass; }
		
		friend UX_CREATE_META_OBJECT( UIFrame,  MetaUIFrame() );


		static void InitWidgetsMetaData ()
		{
		}

		void InitMetaData ()
		{
			Frame::InitMetaData();

			InitWidgetsMetaData();

			CMetaAddIteratorPtr	iter;

			if ( not MetaUIFrame().IsInit() )
			{
				MetaUIFrame().CreateExt( "UIFrame", *this );
				MetaUIFrame().Inherit( Frame::GetMetaClass() );
				iter = MetaUIFrame().GetAddIter( *this );

				iter->AddField( this->_designAxisDirection,	"design_axis_direction" );
			}
		}

		CMetaSetIterator * GetMetaIterator () const
		{
			return MetaUIFrame().GetSetIter( *this );
		}


	// methods
	public:
		UIFrame () : _designAxisDirection(1)
		{}


		void OnLoad (const UILocalization *locale)
		{
			ConvertAxis_t	conv_axis = ( _designAxisDirection == /*UP*/1  ?  &_LB_axis  :  &_LT_axis_to_LB );

			const vec2 &	res		= designResolution;
			const vec2		ver_res	= res.x < res.y ? res : res.yx();
			const vec2		hor_res	= res.x > res.y ? res : res.yx();

			ProcessWidgets( widgets, locale, conv_axis, ver_res, hor_res );
		}


		static void ProcessWidgets (const MetaData::WidgetsVarArray &widgets, const UILocalization *locale,
									ConvertAxis_t convAxis, const vec2 &verRes, const vec2 &horRes)
		{
			FOR( i, (*widgets) )
			{
				WidgetCfg * wgt = (WidgetCfg *) (*widgets)[i].GetPtr();

				convAxis( wgt->layout_horizontal.pos,	horRes );
				convAxis( wgt->layout_vertical.pos,		verRes );

				if ( not wgt->layout_horizontal.enabled and
					 not wgt->layout_vertical.enabled )
				{
					wgt->layout_vertical.enabled = true;
					WARNING( "used deault layout!" );
				}

				ProcessWidgets( wgt->widgets, locale, convAxis, verRes, horRes );

				wgt->ForAllFields( &SetLocaleStringIds, (void *) locale );
			}
		}


		static void SetLocaleStringIds (TypeId_t typeId, void *field, void *param)
		{
			typedef MetaData::ExtLocaleStringCfg	str_t;

			if ( typeId != TypeId< str_t >() or param == null )
				return;

			UILocalization const *	locale	= (const UILocalization *) param;
			str_t &					lstr	= *(str_t *) field;

			locale->ChangeString( lstr.str );
		}


	// hidden methods
	private:
		static vec2 & _LT_axis_to_LB (vec2 &value, const vec2 &size)
		{
			value.y = size.y - value.y;
			return value;
		}

		static vec2 & _LB_axis (vec2 &value, const vec2 &size)
		{
			return value;
		}
	};


}	// Utils
}	// Engine